<template>
    <div>
        <header>
            <van-icon class="i1" name="arrow-left" @click="goout"/>产品详情
        </header>
        <main>
            <div class="first">
                <section>
                    <!-- 轮播图 -->
                    <van-swipe @change="onChange">
                        <van-swipe-item>
                            <img :src="val.productList.img" alt="">
                        </van-swipe-item>
                        <template #indicator>
                          <div class="custom-indicator">{{ current + 1 }}/1</div>
                        </template>
                      </van-swipe>
                </section>
                <h4>
                    {{val.productList.goodsName}}
                    <p>￥<b>{{val.productList.price}}</b></p>
                </h4>
            </div>
            <div class="second">
                <p><span>配送方式&emsp;&emsp;{{val.productList.send}}</span></p>
            </div>
        </main>
        <footer>
            <van-goods-action>
                <router-link to="/index"><van-goods-action-icon icon="wap-home-o" text="主页"/></router-link>
                <router-link to="/shops"><van-goods-action-icon icon="cart-o" text="购物车" /></router-link>
                <van-goods-action-button type="warning" text="加入购物车" @click="showPopup(index=1)"/>
                <van-goods-action-button type="danger" text="立即购买" @click="showPopup(index=2)"/>
              </van-goods-action>

        </footer>
        <van-popup v-model="show" round closeable close-icon="close" position="bottom" :style="{ height: '50%' }">
            <div class="big">
                <article>
                    <div>
                        <img :src="val.productList.img" alt="">
                    </div>
                    <p>￥{{val.productList.price}}
                        <span>/件</span>
                    </p>
                </article>
                <div class="count">
                    <b>购买数量</b>
                    <i>库存{{val.productList.inventory}}件</i>
                    <van-stepper v-model="val.value" integer button-size="36px" input-width="40px" class="stepper"/>
                </div>
                <!-- 携带数据跳转 -->
                <button @click="go2(val)" class="btn">确定</button>
            </div>
        </van-popup>
    </div>
</template>

<script>
import Vue from 'vue';
import { Icon, Toast } from 'vant';
import { Swipe, SwipeItem } from 'vant';
import { Popup } from 'vant';
import { GoodsAction, GoodsActionIcon, GoodsActionButton } from 'vant';
import { Stepper } from 'vant';
import axios from 'axios';
import { Button } from 'vant';

Vue.use(Button);
Vue.use(Stepper);
Vue.use(GoodsAction);
Vue.use(GoodsActionButton);
Vue.use(GoodsActionIcon);
Vue.use(Popup);
Vue.use(Swipe);
Vue.use(SwipeItem);
Vue.use(Icon);
    export default {
        data(){
            return{
                current:0,
                show:false,
                index:0,
                val:{
                    value: 1,
                    productList:{},
                }
            }
        },
        methods: {
            onChange(index) {
                this.current = index;
            },
            showPopup(){
                this.show=true
            },
            async getProduct(){
                let res = await axios({
                    url:"http://192.168.20.106:8000/api/pro/goods?goodsId="+this.$route.query.id,
                    method:'get',
                })
                console.log(res);
                this.val.productList = res.data.data
            },
            goout(){
                this.$router.go(-1)
            },
            go2(val){
                if(this.index==1){
                    axios({
                        url:'http://192.168.20.106:8000/api/cart/add',
                        method:'post',
                        data:{
                            count:this.val.value,
                            productId:this.val.productList.goodsId,
                            price:this.val.productList.price,
                            productImg:this.val.productList.img,
                            productName:this.val.productList.goodsName
                        }
                    }).then(res=>{
                        console.log(res);
                        Toast('添加成功')
                        this.show=false
                    })
                }else{
                    this.$router.push({path:"/submit",query:{val:val}})
                }
            }
        },
        mounted(){
            this.getProduct()
        }
    }
</script>

<style scoped lang="scss">
*{
    margin: 0;
    padding: 0;;
}
header{
    width: 100%;
    height: 44px;
    line-height: 44px;
    background: #fff;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    font-size: 20px;
    color: #333;  
    font-weight: bolder;
    .i1{
        font-size: 20px;
        color: #333;
        margin-right: 10px;
        font-weight: normal;
    }
}
main{
   position: absolute;
   top: 44px;
   left: 0;
   width: 100%;
   height: 100%;
   background: #ccc;
    .first{
        width: 100%;
        background: #fff;
        section{
            width: 100%;
            height: 300px;
            .custom-indicator {
                position: absolute;
                right: 5px;
                bottom: 5px;
                padding: 2px 5px;
                font-size: 12px;
                background: rgba(0, 0, 0, 0.1);
              }
            img{
                display: block;
                width: 100%;
                height: 300px;
            }
        }
        h4{
            margin-top: 10px;
            font-size: 18px;
            color: #000;
            line-height: 1;
            margin-left: 10px;
            margin-bottom: 8px;
            height: 70px;
            p{
                font-size: 14px;
                height: 20px;
                line-height: 1;
                color: #eb0b0b;
                margin-top: 10px;
                b{
                    font-size: 24px;
                    color: #eb0b0b;
                }
            }
        }
    }
    .second{
        width: 100%;
        height: 40px;
        line-height: 40px;
        background-color: #fff;
        p{
            height: 100%;
            color: #333;
            display:flex;
            justify-content: space-between;
            align-items: center;
            span{
                font-size: 12px;
                color: #666;
                margin-left: 15px;
            }
        }
    }
}
footer{
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 44px;
    line-height: 44px;
    z-index: 1;
    background: #fff;
}
.big{
    width: 100%;
    height: 100%;
    padding: 20px;
    box-sizing: border-box;
    img{
        width: 50px;
        height: 50px;
        border-radius: 10px;
    }
    article{
        width: 100%;
        display: flex;
        align-items: center;
        margin-bottom: 20px;
        div{
            text-align: center;
            margin-right: 20px;
        }
        p{
            font-size: 16px;
            color:#eb0b0b;
            font-weight: bolder;
            span{
                font-size: 12px;
                color: #666;
            }
        }
    }
    .count{
        width: 100%;
        height: 44px;
        line-height: 44px;
        display: flex;
        justify-content: space-between;
        b{
            font-size: 16px;
            color: #333;
        }
        i{
            font-style: normal;
            color: #666;
            font-size: 14px;
        }
    }
    .btn{
        width: 100%;
        height: 44px;
        margin-top: 50px;
        border-radius: 22px;
        background: #eb0b0b;
        color: #fff;
        font-size: 16px;
        text-align: center;
        line-height: 44px;
        border: none;
    }
}
</style>